package com.atguigu.eduservice.controller;


import com.atguigu.common.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-01-06
 */
@Api(tags = {"课程管理"})
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @ApiOperation(value = "课程列表(条件查询带分页)")
    @PostMapping("/list/{pageNo}/{pageSize}")
    public R getCourseList(
            @PathVariable("pageNo") Long pageNo,
            @PathVariable("pageSize") Long pageSize,
            @RequestBody EduCourse eduCourse) {
        Map<String, Object> map = eduCourseService.getCourseList(pageNo, pageSize, eduCourse);
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加课程信息")
    @PostMapping("/add")
    public R add(@RequestBody CourseInfoVo courseInfoVo) {
        if (courseInfoVo != null) {
            String courseId = eduCourseService.add(courseInfoVo);
            return R.ok().data("courseId", courseId);
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "根据id获取课程信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo", courseInfoVo);
    }

    @ApiOperation(value = "更新课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        try {
            if (courseInfoVo != null) {
                eduCourseService.updateCourseInfo(courseInfoVo);
                return R.ok();
            }
            return R.error().message("参数不能为null");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error().message("更新失败");
    }

    @ApiOperation(value = "最终课程信息")
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable(value = "id") String id) {
        return R.ok().data("publishCourseInfo", eduCourseService.getPublishCourseInfo(id));
    }

    @ApiOperation(value = "更新课程信息")
    @PostMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable("courseId") String courseId) {
        try {
            eduCourseService.changeStatus(courseId);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error().message("更新失败");
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable("courseId") String courseId) {
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }
}
